package com.memory.monitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.monitor.annotation.AuthCheck;
import com.memory.monitor.common.BaseResponse;
import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ErrorCode;
import com.memory.monitor.common.ResultUtils;
import com.memory.monitor.constant.UserConstant;
import com.memory.monitor.exception.BusinessException;
import com.memory.monitor.exception.ThrowUtils;
import com.memory.monitor.model.dto.product.EstimatedQueryRequest;
import com.memory.monitor.model.dto.product.ImeToMarketQueryRequest;
import com.memory.monitor.model.dto.product.PlantQueryRequest;
import com.memory.monitor.model.dto.product.RecorderQueryRequest;
import com.memory.monitor.model.entity.Product;
import com.memory.monitor.model.entity.Recorder;
import com.memory.monitor.model.vo.ProductVO;
import com.memory.monitor.model.vo.RecorderVO;
import com.memory.monitor.service.ProductService;
import com.memory.monitor.service.RecorderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private RecorderService recorderService;

    /**
     * 查询种植规模
     *
     * @param plantQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/plant/list")
    public BaseResponse<Page<Product>> listPlantByPage(@RequestBody PlantQueryRequest plantQueryRequest,
                                                      HttpServletRequest request) {
        long current = plantQueryRequest.getCurrent();
        long size = plantQueryRequest.getPageSize();
        Page<Product> userPage = productService.page(new Page<>(current, size));
        return ResultUtils.success(userPage);
    }

    /**
     * 删除 plant
     *
     */
    @PostMapping("/plant/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deletePlant(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = productService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 创建 plant
     *
     */
    @PostMapping("/plant/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addPlant(@RequestBody ProductVO productVO, HttpServletRequest request) {
        if (productVO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        boolean result = productService.save(product);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(product.getId());
    }

    /**
     * 更新 plant
     *
     */
    @PostMapping("/plant/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updatePlant(@RequestBody ProductVO productVO,
                                            HttpServletRequest request) {
        if (productVO == null || productVO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        boolean result = productService.updateById(product);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 查询预估产值产量
     *
     * @param estimatedQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/estimate/list")
    public BaseResponse<Page<Product>> listEstimatedByPage(@RequestBody EstimatedQueryRequest estimatedQueryRequest,
                                                      HttpServletRequest request) {
        long current = estimatedQueryRequest.getCurrent();
        long size = estimatedQueryRequest.getPageSize();
        Page<Product> userPage = productService.page(new Page<>(current, size));
        return ResultUtils.success(userPage);
    }

    /**
     * 删除 estimate
     *
     */
    @PostMapping("/estimated/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteEstimated(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = productService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 创建 estimate
     *
     */
    @PostMapping("/estimated/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addEstimated(@RequestBody ProductVO productVO,
                                           HttpServletRequest request) {
        if (productVO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        boolean result = productService.save(product);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(product.getId());
    }

    /**
     * 更新 estimate
     *
     */
    @PostMapping("/estimated/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateEstimated(@RequestBody ProductVO productVO,
                                             HttpServletRequest request) {
        if (productVO == null || productVO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        boolean result = productService.updateById(product);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 查询上市周期
     *
     * @param imetoMarketQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/imeto/list")
    public BaseResponse<Page<Product>> listImetoByPage(@RequestBody ImeToMarketQueryRequest imetoMarketQueryRequest,
                                                      HttpServletRequest request) {
        long current = imetoMarketQueryRequest.getCurrent();
        long size = imetoMarketQueryRequest.getPageSize();
        Page<Product> userPage = productService.page(new Page<>(current, size));
        return ResultUtils.success(userPage);
    }

    /**
     * 删除 imeto
     *
     */
    @PostMapping("/imeto/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteImeto(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = productService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 创建 imeto
     *
     */
    @PostMapping("/imeto/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addImeto(@RequestBody ProductVO productVO, HttpServletRequest request) {
        if (productVO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        boolean result = productService.save(product);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(product.getId());
    }

    /**
     * 更新 imeto
     *
     */
    @PostMapping("/imeto/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateImeto(@RequestBody ProductVO productVO,
                                             HttpServletRequest request) {
        if (productVO == null || productVO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        boolean result = productService.updateById(product);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 查询录像机
     *
     * @param recorderQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/recorder/list")
    public BaseResponse<Page<Recorder>> listRecorderByPage(@RequestBody RecorderQueryRequest recorderQueryRequest,
                                                           HttpServletRequest request) {
        long current = recorderQueryRequest.getCurrent();
        long size = recorderQueryRequest.getPageSize();
        Page<Recorder> userPage = recorderService.page(new Page<>(current, size));
        return ResultUtils.success(userPage);
    }

    /**
     * 删除 video
     *
     */
    @PostMapping("/recorder/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteRecorder(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = productService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 创建 video
     *
     */
    @PostMapping("/recorder/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addRecorder(@RequestBody RecorderVO recorderVO, HttpServletRequest request) {
        if (recorderVO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Recorder recorder = new Recorder();
        BeanUtils.copyProperties(recorderVO, recorder);
        boolean result = recorderService.save(recorder);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(recorder.getId());
    }

    /**
     * 更新 video
     *
     */
    @PostMapping("/recorder/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateRecorder(@RequestBody RecorderVO recorderVO,
                                             HttpServletRequest request) {
        if (recorderVO == null || recorderVO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Recorder recorder = new Recorder();
        BeanUtils.copyProperties(recorderVO, recorder);
        boolean result = recorderService.updateById(recorder);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }


}